<%@ page contentType="text/html;charset=UTF-8"%>
<%@ include file="/common/taglibs.jsp"%>
<c:set value="recruit" var="moduleName" scope="request" />
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
	<head>
		<title>在线申请 - 股权结构</title>
	</head>
	<body>
		<html:form action="/site/user/applyEquity.do" styleId="applyEquityForm" method="POST">
			<input type="hidden" name="method" class="method" value="save" />
			<html:hidden property="id" />
			<html:hidden property="applyBaseId" />
			<input type="hidden" name="direction" id="direction" value="" />
			<div class="site-title">
				三、 股权结构
			</div>
			<br />
			<div class="div-content">
			<table class="table-layout">
				<tr class="tr-even">
					<td>
						如果申请公司是国有独资公司，请填写上级单位：
						<html:text property="parentCorp" style="width:500px" />
					</td>
				</tr>
				<tr class="tr-odd">
					<td>
						如果申请公司是有限责任公司或股份有限公司，请填写所有股东的相关信息：

					</td>
				</tr>
				<tr class="tr-even">
					<td>
						<table class="table-layout" id="tableShareholderA">
							<thead>
								<tr class="tr-even">
									<th>
										股东
									</th>
									<th style="width:150px;">
										投入金额（万元）
									</th>
									<th style="width:150px;">
										所占比例（%）

									</th>
									<th style="width:100px;">
										操作
									</th>
								</tr>
							</thead>
							<tbody id="tbodyShareholderA"></tbody>
						</table>
						<div id="divShareholderAFormContainer">
						</div>
						<br />
						<div class="center-opertion">
							<input type="button" value="添加" onclick="preAddShareholderA()" />
						</div>
					</td>
				</tr>
				<tr class="tr-odd">
					<td>
						如果申请公司的第一股东为企业法人，请填写以下企业法人的信息：

					</td>
				</tr>
				<tr class="tr-even">
					<td>
						<table class="table-layout" id="tableShareholderB">
							<thead>
								<tr class="tr-even">
									<th>
										股东
									</th>
									<th style="width:150px;">
										投入金额（万元）
									</th>
									<th style="width:150px;">
										所占比例（%）

									</th>
									<th style="width:100px;">
										操作
									</th>
								</tr>
							</thead>
							<tbody id="tbodyShareholderB"></tbody>
						</table>
						<div id="divShareholderBFormContainer">
						</div>
						<br />
						<div class="center-opertion">
							<input type="button" value="添加" onclick="preAddShareholderB()" />
						</div>
					</td>
				</tr>
			</table>
			<br />
			<div class="center-opertion">
				<html:submit>保存</html:submit>
				<input type="button" value="上一步" onclick="toStep(-1)" />
				<input type="button" value="下一步" onclick="toStep(1)" />
				<html:cancel>取消</html:cancel>
			</div>
			</div>
		</html:form>
		<form action="<c:url value="/site/user/shareholderA.do?method=ajaxSave" />" method="POST" id="shareholderAForm" style="display:none;" target="hiddenFrame">
			<input type="hidden" name="id" value=""/>
			<input type="hidden" name="applyEquityId" value="<c:out value="${applyEquityForm.id}" />" id="currentEquityId" />
			<table class="table-layout">
				<tr class="tr-odd">
					<td>
						<input type="text" name="shareholderName" />
					</td>
					<td style="width:150px;">
						<input type="text" name="stake"  onkeyup="calPercent('A')"/>
					</td>
					<td style="width:150px;">
						<input type="text" name="percent" readonly="true" class="input-readonly"/>
					</td>
					<td style="width:100px;">
						<input type="submit" value="提交" />
						<input type="button" value="取消" onclick="hideShareholderA()" />
					</td>
				</tr>
			</table>
		</form>
		<form action="<c:url value="/site/user/shareholderB.do?method=ajaxSave" />" method="POST" id="shareholderBForm" style="display:none;" target="hiddenFrame">
			<input type="hidden" name="id"  value=""/>
			<input type="hidden" name="applyEquityId" value="<c:out value="${applyEquityForm.id}" />" id="currentEquityId" />
			<table class="table-layout">
				<tr class="tr-odd">
					<td>
						<input type="text" name="shareholderName" />
					</td>
					<td style="width:150px;">
						<input type="text" name="stake" onkeyup="calPercent('B')"/>
					</td>
					<td style="width:150px;">
						<input type="text" name="percent" readonly="true" class="input-readonly"/>
					</td>
					<td style="width:100px;">
						<input type="submit" value="提交" />
						<input type="button" value="取消" onclick="hideShareholderB()" />
					</td>
				</tr>
			</table>
		</form>
		<iframe name="hiddenFrame" style="display:none;"></iframe>
		<script type="text/javascript">
			function validate100(tbodyId){
				var ts = _g(tbodyId).childNodes;
				var sum = 0;
				/*if(ts && ts.length > 0){
					for(var i = 0;i< ts.length;i++){
						x = new Number(ts[i].childNodes[2].innerHTML);
						if(!isNaN(x)) sum += x;
					}
				}			
				if(sum > 100){
					alert("股份所占比例之和不能大于100。")
					return false;
				}*/
				return true;
			}
			function validate(){
				if(validate100("tbodyShareholderA") && validate100("tbodyShareholderB"))
					_g("applyEquityForm").submit();
			}
					
			function toStep(num){
				if(validate100("tbodyShareholderA") && validate100("tbodyShareholderB")){
					if(num > 0)
						_g("direction").value="next";
					else if(num < 0)
						_g("direction").value="last";
					_g("applyEquityForm").submit();
				}
			}
			
			function getShareHolderRecord(flag){
				var rec=_g('tbodyShareholder'+flag);
				return rec;
			}
			
			
			var applyEquityIdVal = _g("currentEquityId").value;
			
			//shareholderA
			var shareholderAUrl = "<c:url value='shareholderA.do' />";
			var shareholderAFormEl = _g("shareholderAForm");
			var shareholderAFlag=false;
			function preAddShareholderA(){	
				if(applyEquityIdVal == null || applyEquityIdVal.length < 1)
					alert("请先保存当前表单，才能添加股东。");
				else if(!shareholderAFlag){
					showShareholderA();				
				}else{
					shareholderAFormEl.submit();		
				}
			}
			
			function resetShareholderA(){
				shareholderAFormEl.reset();
				shareholderAFormEl["id"].value="";
			}
			
			function showShareholderA(){	
				resetShareholderA();
				_g("divShareholderAFormContainer").appendChild(shareholderAFormEl);
				shareholderAFormEl.style.display="";
				shareholderAFlag=true;
			}
			
			function hideShareholderA(){
				resetShareholderA();
				shareholderAFormEl.style.display = "none";
				_g('bd').appendChild(shareholderAFormEl);
				shareholderAFlag=false;
				calPercent('A');
			}
			
			function editShareholderA(id){
				shareholderAFlag=true;
				_ajax(shareholderAUrl + "?method=ajaxView&id=" + id,function(xhr){
					var shareholderA = JSON.parse(xhr.responseText);
					if(!refreshMessage(shareholderA)){ shareholderAFlag=true;return false;}
					shareholderA = shareholderA.data;
					
					showShareholderA();
					shareholderAFormEl.id.value = shareholderA.id;
					shareholderAFormEl.shareholderName.value = shareholderA.shareholderName;
					shareholderAFormEl.stake.value = shareholderA.stake;
					shareholderAFormEl.percent.value = shareholderA.percent;
				})
			}
			
			function successShareholderA(jsonString){
				if(!refreshMessageJsonString(jsonString)){ shareholderAFlag=true;return false;}
				hideShareholderA();
				listShareholderA();	
				showShareholderA();
			}
			
			function deleteShareholderA(id){
				if(confirm("确定要删除该记录吗？")) 
					_ajax(shareholderAUrl + "?method=ajaxErase&id="+id, function(xhr){
						var data = JSON.parse(xhr.responseText);
						if(!refreshMessage(data)) return false;
						
						listShareholderA();
					})
				calPercent('A');
			}
			function calPercent(flag){
				var formName="shareholder"+flag+"Form";
				var stake=_g(formName)["stake"].value;
				var rec=getShareHolderRecord(flag).childNodes;
				var total=0;	
				var shareID=_g(formName)["id"].value;	
				if(shareID=="")
					total=getTotalInfo(rec,null)*1.0+stake*1.0;	
				else	
					total=getTotalInfo(rec,shareID)*1.0+stake*1.0;	
				if(total==0||rec.length==0){
					_g(formName)["percent"].value="100";
				}else{
					for(var i=0;i<rec.length;i++){
						var tr=rec[i];
						var tempstake=tr.childNodes[1].firstChild.nodeValue*1;
						var temppercent=tr.childNodes[2].firstChild;	
						var tempid=tr.childNodes[3].lastChild.firstChild.nodeValue;
						if(shareID==""||shareID!=tempid)
							temppercent.nodeValue=total==0?0:Math.round(((tempstake/total)*10000))/100;
			
					}
				}
				_g(formName)["percent"].value=total==0?0:Math.round(((stake/total)*10000))/100;;
			}	
			
			function getTotalInfo(rec,id){
				var total=0;	
				for(var i=0;i<rec.length;i++){
					var tr=rec[i];
					var temp=tr.childNodes[1].firstChild.nodeValue;
					var tempid=tr.childNodes[3].lastChild.firstChild.nodeValue;
					if(id==null||id!=tempid)
						total=total*1+temp*1;		
				}	
				return total;
			}
			
			function listShareholderA(){
				if(applyEquityIdVal != null && applyEquityIdVal.length > 0)
				_ajax(shareholderAUrl + "?method=ajaxList&applyEquityId=" + applyEquityIdVal,function(xhr){
					var shareholderAList = JSON.parse(xhr.responseText);
					if(!refreshMessage(shareholderAList,false)) return false;
					shareholderAList = shareholderAList.data;
					
					var listHtml = '<table><tbody id="tbodyShareholderA">';
					if(shareholderAList && shareholderAList.length)
					for(var i = 0;i < shareholderAList.length;i++){
						listHtml += '<tr class="'+(i%2 == 0 ? 'tr-odd' : 'tr-even')+'">';
						listHtml += '<td>'+shareholderAList[i].shareholderName+'</td>';
						listHtml += '<td>'+shareholderAList[i].stake+'</td>';
						listHtml += '<td>'+shareholderAList[i].percent+'</td>';
						listHtml += '<td>';			
						listHtml += '<a href="javascript:void(0);" onclick="javascript:editShareholderA('+shareholderAList[i].id+');">修改</a>&nbsp;';
						listHtml += '<a href="javascript:void(0)" onclick="javascript:deleteShareholderA('+shareholderAList[i].id+');">删除</a>&nbsp;';
						listHtml += '<span style="display:none">';
						listHtml += shareholderAList[i].id;
						listHtml += '</span>';
						listHtml += '</td>';
						listHtml += '</tr>';
					}
					listHtml += '</tbody></table>';
					equalInnerHTML(listHtml,function(tbody){
						var t = _g("tableShareholderA");
						var tb =  _g("tbodyShareholderA");
						t.removeChild(tb);
						t.appendChild(tbody);
						calPercent('A');
					});
				})
			}
			listShareholderA();
			
			//shareholderB
			var shareholderBUrl = "<c:url value='shareholderB.do' />";
			var shareholderBFormEl = _g("shareholderBForm");
			var shareholderBFlag=false;
			function preAddShareholderB(){
				if(applyEquityIdVal == null || applyEquityIdVal.length < 1)
					alert("请先保存当前表单，才能添加股东。")
				else if(!shareholderBFlag){
					showShareholderB();
				}else{
					shareholderBFormEl.submit();		
				}
			}
			
			function resetShareholderB(){
				shareholderBFormEl.reset();
				shareholderBFormEl.id.value="";
			}
			
			function showShareholderB(){
				resetShareholderB();
				_g("divShareholderBFormContainer").appendChild(shareholderBFormEl);
				shareholderBFormEl.style.display="";
				shareholderBFlag=true;
			}
			
			function hideShareholderB(){
				resetShareholderB();
				shareholderBFormEl.style.display = "none";
				_g('bd').appendChild(shareholderBFormEl);
				shareholderBFlag=false;
				calPercent('B');
			}
			
			function editShareholderB(id){
				shareholderBFlag=true;
				_ajax(shareholderBUrl + "?method=ajaxView&id=" + id,function(xhr){
					var shareholderB = JSON.parse(xhr.responseText);
					if(!refreshMessage(shareholderB)) return false;
					shareholderB = shareholderB.data;
					
					showShareholderB();
					shareholderBFormEl.id.value = shareholderB.id;
					shareholderBFormEl.shareholderName.value = shareholderB.shareholderName;
					shareholderBFormEl.stake.value = shareholderB.stake;
					shareholderBFormEl.percent.value = shareholderB.percent;
				})
			}
			
			function successShareholderB(jsonString){
				if(!refreshMessageJsonString(jsonString)) { shareholderBFlag=true;return false;}
				hideShareholderB();
				listShareholderB();
				showShareholderB();
			}
			
			function deleteShareholderB(id){
				if(confirm("确定要删除该记录吗？")) 
					_ajax(shareholderBUrl + "?method=ajaxErase&id="+id, function(xhr){
						var data = JSON.parse(xhr.responseText);
						if(!refreshMessage(data)) return false;
						
						listShareholderB();
					})
				calPercent('B');
			}
			
			function listShareholderB(){
				if(applyEquityIdVal != null && applyEquityIdVal.length > 0)
				_ajax(shareholderBUrl + "?method=ajaxList&applyEquityId=" + applyEquityIdVal,function(xhr){
					var shareholderBList = JSON.parse(xhr.responseText);
					if(!refreshMessage(shareholderBList,false)) return false;
					shareholderBList = shareholderBList.data;
			
					var listHtml = '<table><tbody id="tbodyShareholderB">';
					if(shareholderBList && shareholderBList.length)
					for(var i = 0;i < shareholderBList.length;i++){
						listHtml += '<tr class="'+(i%2 == 0 ? 'tr-odd' : 'tr-even')+'">';
						listHtml += '<td>'+shareholderBList[i].shareholderName+'</td>';
						listHtml += '<td>'+shareholderBList[i].stake+'</td>';
						listHtml += '<td>'+shareholderBList[i].percent+'</td>';
						listHtml += '<td>';
						listHtml += '<a href="javascript:void(0);" onclick="javascript:editShareholderB('+shareholderBList[i].id+');">修改</a>&nbsp;';
						listHtml += '<a href="javascript:void(0)" onclick="javascript:deleteShareholderB('+shareholderBList[i].id+');">删除</a>&nbsp;';
						listHtml += '<span style="display:none">';
						listHtml += shareholderBList[i].id;
						listHtml += '</span>';
						listHtml += '</td>';						
						listHtml += '</tr>';
					}
					listHtml += '</tbody></table>';
					equalInnerHTML(listHtml,function(tbody){
						var t = _g("tableShareholderB");
						var tb =  _g("tbodyShareholderB");
						t.removeChild(tb);
						t.appendChild(tbody);
						calPercent('B');
					})
				})
			}
			listShareholderB();
		</script>
	</body>
</html>
